/**
 * Sensor Network Development Kit for 1-Wire Network (SeN)
 * Version : 0.1
 * Revision date : 2008-10-13
 * Author: Dubravko Penezic, dpenezic@gmail.com
 * 
 * Based on T. Bitson, SimpleWeather, TempSensor.java, 2007
 */

package owSensor;


import com.dalsemi.onewire.*;
import com.dalsemi.onewire.adapter.*;
import com.dalsemi.onewire.container.*;
import owLib.*;
import utilityLib.TemperatureTransformer;

/**
 * TempSensor class implement method to read DS1820/DS18S20/DS1920 temperature sensor and manage resolution
 * Version : 0.1.1
 * Revision date : 2008-10-20
 * 
 * @author Dubravko Penezic, dpenezic@gmail.com
 *
 */

public class TempSensor
{
  // class variables
  private DSPortAdapter adapter;
  private OWDevice device;
  private OneWireContainer10 tempDevice = null;
  private static boolean debugFlag = OWConf.debugFlag;
  private double tResolution = OneWireContainer10.RESOLUTION_MAXIMUM;
  private OWCommPath p;
  
  /**
   * TempSensor method configure class parameters and create soft link to 1-Wire device
   *   
   * @param adapter
   * @param device
   */
  public TempSensor(DSPortAdapter adapter, OWDevice device)
  {
    this.adapter = adapter;
    this.device = device;
    this.p = new OWCommPath(this.adapter, this.device);
        
    // get instances of the 1-wire devices
    tempDevice = new OneWireContainer10(adapter, device.getDAddress());
    
    p.activatePath();
    
    try
    {
      if (tempDevice.hasSelectableTemperatureResolution())
      {
        // set resolution to max
        byte[] state = tempDevice.readDevice();
        tempDevice.setTemperatureResolution(this.tResolution, state);
        tempDevice.writeDevice(state);
        
        if (debugFlag)
          System.out.println("Temp Device Supports High Resolution");
      }
    }
    catch (OneWireException e)
    {
      System.out.println("Error Setting Resolution: " + e);
    }
    p.deactivatePath();
  }
   
  /**
   * getTemperature method return temperature readed from DS sensor on scale predefined in class
   *   
   * @return
   */  
  public float getTemperature()
  {
    float temperature = -999.9f;
    
    // make sure the temp device instance is not null
    if (tempDevice != null)
    {
      
      if (debugFlag)
      {
        System.out.print("Temperature: Device = " + tempDevice.getName());
        System.out.print("  ID = " + tempDevice.getAddressAsString() + "\n");
      }
      
      p.activatePath();
      try
      {
        byte[] state = tempDevice.readDevice();
        tempDevice.doTemperatureConvert(state);
        
        state = tempDevice.readDevice();
        temperature = (float)tempDevice.getTemperature(state);
        
      }
      catch (OneWireException e)
      {
        System.out.println("Error Reading Temperature: " + e);
      }
      p.deactivatePath();
    }
       
    return temperature;
  }
    
  /**
   * setTemperatureResolution method set temperature resolution ("normal", "maximum") for reading data from temperature sensor
   * 
   * @param tr
   */
  public void setTemperatureResolution(String tr){
	  
	  if(tr.equalsIgnoreCase("normal")) tResolution = OneWireContainer10.RESOLUTION_NORMAL;
	  if(tr.equalsIgnoreCase("maximum")) tResolution = OneWireContainer10.RESOLUTION_NORMAL;

	  p.activatePath();

	  try
	  {
		  if (tempDevice.hasSelectableTemperatureResolution())
		  {
			  // set resolution to max
			  byte[] state = tempDevice.readDevice();
			  tempDevice.setTemperatureResolution(this.tResolution, state);
			  tempDevice.writeDevice(state);

			  if (debugFlag)
				  System.out.println("Temp Device Supports High Resolution");
		  }
	  }
	  catch (OneWireException e)
	  {
		  System.out.println("Error Setting Resolution: " + e);
	  }
	  p.deactivatePath();

  }
}
